﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.Models;
using zhangmenren.IService;
using Newtonsoft.Json;
using zhangmenren.Models.ViewModle;
using zhangmenren.Models.Enums;

namespace zhangmenren.Areas.Admin.Controllers
{
    [Area(areaName: "Admin")]
    public class WorksController : Controller
    {
        private IWorksService worksService;
        private IUserService userService;
        private IClassMSGService classMSGService;
        public WorksController(IWorksService _worksService,IUserService _userService,IClassMSGService _classMSGService)
        {
            worksService = _worksService;
            userService = _userService;
            classMSGService = _classMSGService;
        }

        /// <summary>
        /// 作品控制器
        /// </summary>
        /// <returns>The index.</returns>
        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Add()
        {
           
            ViewBag.UserStr = await GetUser();
            ViewBag.ClassStr = await GetClass();
            return View();
        }


        [HttpPost]
        public async Task< JsonResult> Add(WorksEntity entity)
        {
            if (entity != null)
            {
                var result = await worksService.Add(entity);
                return Json(new { State = "OK", Msg = "添加成功" });
            }
            return Json(new { State = "FAIL", Msg = "添加错误，请稍后重试。" });
        }


        [HttpPost]
        public async Task< JsonResult> IsEnabled(bool isEnabled, int id = 0)
        {
            if (id != 0)
            {
                var result = await worksService.EditState(id, isEnabled);
                return Json(new { code = 1, msg = "修改成功" });
            }
            return Json(new { code = 0, msg = "标识错误" });
        }

        [HttpPost]
        public async  Task < JsonResult> EditSort(int sort, int id = 0)
        {
            if (id != 0)
            {
                var result = await worksService.EditSort(id,sort);
                return Json(new { code = 1, msg = "修改成功" });
            }
            return Json(new { code = 0, msg = "标识错误" });
        }

        [HttpPost]
        public async Task< JsonResult> IsXianShi(bool isrecommend, int id = 0)
        {
            if (id != 0)
            {
                var result = await worksService.IsXianShi(id, isrecommend);
                return Json(new { code = 1, msg = "修改成功" });
            }
            return Json(new {code=0, msg = "标识错误" });
        }


        public async Task<string> GetWork(string content="", int page = 1)
        {
            int total = 0;
            IEnumerable<WorksModel> dataList = null;
            dataList = await worksService.Selelct(page,50, content);
            dataList.All(p =>
            {

                p.TypeStr = EnumsHelp.GetDisplayName(p.Type); 
                return true;
            });

            if (dataList.Any())
            {
                total = dataList.Count();
            }
            return JsonConvert.SerializeObject(new { code = "0", msg = "查询成功", data = dataList, count = total });

        }

        [HttpPost]
        public async Task<JsonResult> Del(int id = 0)
        {
            if (id != 0)
            {
                var result = await worksService.Del(id);
                return Json(new { state = "ok", msg = "删除成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }


        /// <summary>
        /// 获取掌门号不为空的用户
        /// </summary>
        /// <returns>The user.</returns>
        public  async Task<string> GetUser()
        {
            var list = await userService.Find();
            string html = "<option value=''>请选择</option>";
            if (list.Any())
            {
                foreach (var item in list)
                {
                    html += "<option value='" + item.Id + "'>" + item.UserName + "</option>";
                }
            }
            return html;
        }

        /// <summary>
        /// 获取作品分类
        /// </summary>
        /// <returns>The user.</returns>
        public async Task<string> GetClass()
        {
            var list = await classMSGService.Select();
            string html = "<option value=''>请选择</option>";
            if (list.Any())
            {
                foreach (var item in list)
                {
                    html += "<option value='" + item.Id + "'>" + item.Title + "</option>";
                }
            }
            return html;
        }

    }
}
